{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "adf80336",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d53ff617",
   "metadata": {},
   "source": [
    "# 按值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c8dcd319",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1=pd.DataFrame({'first':[2,3,8],\n",
    "                'second':[4,5,6]},\n",
    "               index=['aa','a','c'])\n",
    "\n",
    "df2=pd.DataFrame({'second':[6,5,8],\n",
    "                'third':[17,8,19]},\n",
    "               index=['b','c','aa'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "589f9b2b",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th>third</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>6</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   first  second  third\n",
       "0      3       5      8\n",
       "1      8       6     17"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1,df2,how='inner')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "31bd5e1c",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th>third</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8.0</td>\n",
       "      <td>6</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>8</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   first  second  third\n",
       "0    2.0       4    NaN\n",
       "1    3.0       5    8.0\n",
       "2    8.0       6   17.0\n",
       "3    NaN       8   19.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1,df2,how='outer')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "146a54ed",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th>third</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>6</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   first  second  third\n",
       "0      2       4    NaN\n",
       "1      3       5    8.0\n",
       "2      8       6   17.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1,df2,how='left')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7dc48de0",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th>third</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8.0</td>\n",
       "      <td>6</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>8</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   first  second  third\n",
       "0    8.0       6     17\n",
       "1    3.0       5      8\n",
       "2    NaN       8     19"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1,df2,how='right')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e15c6226",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>second_x</th>\n",
       "      <th>second_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   first  second_x  second_y\n",
       "0     17       NaN         6\n",
       "1      8       6.0         5\n",
       "2     19       NaN         8"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1,df2,how='right',on='first')  # 多个相同列指定关联列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "93cd9258",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>second_df1</th>\n",
       "      <th>second_df2</th>\n",
       "      <th>third</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>8</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   first  second_df1  second_df2  third\n",
       "0    NaN         NaN           6     17\n",
       "1    NaN         NaN           5      8\n",
       "2    8.0         6.0           8     19"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 拼合不用列明的列\n",
    "pd.merge(df1,df2,how='right',\n",
    "         left_on='first',right_on='second',\n",
    "        suffixes=('_df1','_df2'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8a493fba",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>second_df1</th>\n",
       "      <th>second_df2</th>\n",
       "      <th>third</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>aa</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    first  second_df1  second_df2  third\n",
       "b     NaN         NaN           6     17\n",
       "c     8.0         6.0           5      8\n",
       "aa    2.0         4.0           8     19"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1,df2,how='right',\n",
    "         left_index=True,right_index=True,\n",
    "         suffixes=('_df1','_df2'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c14b9ab8",
   "metadata": {},
   "source": [
    "# 分组聚合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a13e0cdb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>订单号</th>\n",
       "      <th>销售日期</th>\n",
       "      <th>销售人员</th>\n",
       "      <th>地区</th>\n",
       "      <th>城市</th>\n",
       "      <th>家电品牌</th>\n",
       "      <th>单价</th>\n",
       "      <th>数量（台）</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10240</td>\n",
       "      <td>2009-01-02</td>\n",
       "      <td>张三</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10241</td>\n",
       "      <td>2009-01-03</td>\n",
       "      <td>李四</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>格力</td>\n",
       "      <td>1300</td>\n",
       "      <td>5</td>\n",
       "      <td>6500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10242</td>\n",
       "      <td>2009-01-13</td>\n",
       "      <td>钱五</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>美的</td>\n",
       "      <td>1250</td>\n",
       "      <td>6</td>\n",
       "      <td>7500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10243</td>\n",
       "      <td>2009-01-14</td>\n",
       "      <td>赵六</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1500</td>\n",
       "      <td>3</td>\n",
       "      <td>4500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10244</td>\n",
       "      <td>2009-01-25</td>\n",
       "      <td>刘琦</td>\n",
       "      <td>华北</td>\n",
       "      <td>石家庄</td>\n",
       "      <td>海尔</td>\n",
       "      <td>1500</td>\n",
       "      <td>5</td>\n",
       "      <td>7500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     订单号       销售日期 销售人员  地区   城市 家电品牌    单价  数量（台）   销售额\n",
       "0  10240 2009-01-02   张三  华北   北京  奥克斯  1200      4  4800\n",
       "1  10241 2009-01-03   李四  华北   北京   格力  1300      5  6500\n",
       "2  10242 2009-01-13   钱五  华北   北京   美的  1250      6  7500\n",
       "3  10243 2009-01-14   赵六  华北   北京   春兰  1500      3  4500\n",
       "4  10244 2009-01-25   刘琦  华北  石家庄   海尔  1500      5  7500"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=pd.read_excel('Excel数据.xlsx')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bac16d3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['华北', '华南', '西南', '西北', '华中', '华东', '东北'], dtype=object)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['地区'].unique() # 去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "cd9ec2af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "地区\n",
       "东北    100105\n",
       "华东    113550\n",
       "华中     53100\n",
       "华北    105200\n",
       "华南    126200\n",
       "西北    118300\n",
       "西南    124814\n",
       "Name: 销售额, dtype: int64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='地区')['销售额'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "98c34a8e",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>数量（台）</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>东北</th>\n",
       "      <td>100105</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华东</th>\n",
       "      <td>113550</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华中</th>\n",
       "      <td>53100</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华北</th>\n",
       "      <td>105200</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华南</th>\n",
       "      <td>126200</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西北</th>\n",
       "      <td>118300</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西南</th>\n",
       "      <td>124814</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       销售额  数量（台）\n",
       "地区               \n",
       "东北  100105     76\n",
       "华东  113550     85\n",
       "华中   53100     33\n",
       "华北  105200     70\n",
       "华南  126200     84\n",
       "西北  118300     87\n",
       "西南  124814     95"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='地区')[['销售额','数量（台）']].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "b8ab6030",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>数量（台）</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区</th>\n",
       "      <th>城市</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">东北</th>\n",
       "      <th>哈尔滨</th>\n",
       "      <td>43000</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>沈阳</th>\n",
       "      <td>27405</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>长春</th>\n",
       "      <td>29700</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">华东</th>\n",
       "      <th>上海</th>\n",
       "      <td>60950</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>杭州</th>\n",
       "      <td>21100</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>苏州</th>\n",
       "      <td>31500</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">华中</th>\n",
       "      <th>武汉</th>\n",
       "      <td>12400</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>长沙</th>\n",
       "      <td>40700</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">华北</th>\n",
       "      <th>北京</th>\n",
       "      <td>53900</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>石家庄</th>\n",
       "      <td>51300</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">华南</th>\n",
       "      <th>厦门</th>\n",
       "      <td>86200</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>广州</th>\n",
       "      <td>40000</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">西北</th>\n",
       "      <th>乌鲁木齐</th>\n",
       "      <td>50100</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>兰州</th>\n",
       "      <td>68200</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">西南</th>\n",
       "      <th>成都</th>\n",
       "      <td>58907</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>昆明</th>\n",
       "      <td>45500</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>重庆</th>\n",
       "      <td>20407</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           销售额  数量（台）\n",
       "地区 城市                \n",
       "东北 哈尔滨   43000     35\n",
       "   沈阳    27405     20\n",
       "   长春    29700     21\n",
       "华东 上海    60950     46\n",
       "   杭州    21100     15\n",
       "   苏州    31500     24\n",
       "华中 武汉    12400     10\n",
       "   长沙    40700     23\n",
       "华北 北京    53900     36\n",
       "   石家庄   51300     34\n",
       "华南 厦门    86200     55\n",
       "   广州    40000     29\n",
       "西北 乌鲁木齐  50100     35\n",
       "   兰州    68200     52\n",
       "西南 成都    58907     41\n",
       "   昆明    45500     38\n",
       "   重庆    20407     16"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by=['地区','城市'])[['销售额','数量（台）']].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "56e90cb2",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>数量（台）</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区</th>\n",
       "      <th>城市</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">东北</th>\n",
       "      <th>哈尔滨</th>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>沈阳</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>长春</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">华东</th>\n",
       "      <th>上海</th>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>杭州</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>苏州</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">华中</th>\n",
       "      <th>武汉</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>长沙</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">华北</th>\n",
       "      <th>北京</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>石家庄</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">华南</th>\n",
       "      <th>厦门</th>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>广州</th>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">西北</th>\n",
       "      <th>乌鲁木齐</th>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>兰州</th>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">西南</th>\n",
       "      <th>成都</th>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>昆明</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>重庆</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         销售额  数量（台）\n",
       "地区 城市              \n",
       "东北 哈尔滨     7      7\n",
       "   沈阳      4      4\n",
       "   长春      3      3\n",
       "华东 上海      8      8\n",
       "   杭州      4      4\n",
       "   苏州      4      4\n",
       "华中 武汉      4      4\n",
       "   长沙      3      3\n",
       "华北 北京      6      6\n",
       "   石家庄     6      6\n",
       "华南 厦门      9      9\n",
       "   广州      9      9\n",
       "西北 乌鲁木齐    8      8\n",
       "   兰州      8      8\n",
       "西南 成都      7      7\n",
       "   昆明      6      6\n",
       "   重庆      3      3"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by=['地区','城市'])[['销售额','数量（台）']].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "9dec7296",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"6\" halign=\"left\">销售额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>mean</th>\n",
       "      <th>count</th>\n",
       "      <th>var</th>\n",
       "      <th>std</th>\n",
       "      <th>median</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>东北</th>\n",
       "      <td>100105</td>\n",
       "      <td>7150.357143</td>\n",
       "      <td>14</td>\n",
       "      <td>9.488733e+06</td>\n",
       "      <td>3080.378638</td>\n",
       "      <td>6502.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华东</th>\n",
       "      <td>113550</td>\n",
       "      <td>7096.875000</td>\n",
       "      <td>16</td>\n",
       "      <td>2.050482e+07</td>\n",
       "      <td>4528.225140</td>\n",
       "      <td>5000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华中</th>\n",
       "      <td>53100</td>\n",
       "      <td>7585.714286</td>\n",
       "      <td>7</td>\n",
       "      <td>5.489476e+07</td>\n",
       "      <td>7409.099939</td>\n",
       "      <td>4800.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华北</th>\n",
       "      <td>105200</td>\n",
       "      <td>8766.666667</td>\n",
       "      <td>12</td>\n",
       "      <td>2.646061e+07</td>\n",
       "      <td>5143.987370</td>\n",
       "      <td>7500.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华南</th>\n",
       "      <td>126200</td>\n",
       "      <td>7011.111111</td>\n",
       "      <td>18</td>\n",
       "      <td>2.694340e+07</td>\n",
       "      <td>5190.703102</td>\n",
       "      <td>5500.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西北</th>\n",
       "      <td>118300</td>\n",
       "      <td>7393.750000</td>\n",
       "      <td>16</td>\n",
       "      <td>2.268196e+07</td>\n",
       "      <td>4762.557961</td>\n",
       "      <td>5800.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西南</th>\n",
       "      <td>124814</td>\n",
       "      <td>7800.875000</td>\n",
       "      <td>16</td>\n",
       "      <td>1.137509e+07</td>\n",
       "      <td>3372.697098</td>\n",
       "      <td>7203.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       销售额                                                      \n",
       "       sum         mean count           var          std  median\n",
       "地区                                                              \n",
       "东北  100105  7150.357143    14  9.488733e+06  3080.378638  6502.5\n",
       "华东  113550  7096.875000    16  2.050482e+07  4528.225140  5000.0\n",
       "华中   53100  7585.714286     7  5.489476e+07  7409.099939  4800.0\n",
       "华北  105200  8766.666667    12  2.646061e+07  5143.987370  7500.0\n",
       "华南  126200  7011.111111    18  2.694340e+07  5190.703102  5500.0\n",
       "西北  118300  7393.750000    16  2.268196e+07  4762.557961  5800.0\n",
       "西南  124814  7800.875000    16  1.137509e+07  3372.697098  7203.5"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='地区').agg({'销售额':['sum','mean','count','var','std','median']})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "3fcb8183",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">销售额</th>\n",
       "      <th colspan=\"2\" halign=\"left\">数量（台）</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>sum</th>\n",
       "      <th>sum</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>东北</th>\n",
       "      <td>7150.357143</td>\n",
       "      <td>100105</td>\n",
       "      <td>76</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华东</th>\n",
       "      <td>7096.875000</td>\n",
       "      <td>113550</td>\n",
       "      <td>85</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华中</th>\n",
       "      <td>7585.714286</td>\n",
       "      <td>53100</td>\n",
       "      <td>33</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华北</th>\n",
       "      <td>8766.666667</td>\n",
       "      <td>105200</td>\n",
       "      <td>70</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华南</th>\n",
       "      <td>7011.111111</td>\n",
       "      <td>126200</td>\n",
       "      <td>84</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西北</th>\n",
       "      <td>7393.750000</td>\n",
       "      <td>118300</td>\n",
       "      <td>87</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西南</th>\n",
       "      <td>7800.875000</td>\n",
       "      <td>124814</td>\n",
       "      <td>95</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            销售额         数量（台）      \n",
       "           mean     sum   sum count\n",
       "地区                                 \n",
       "东北  7150.357143  100105    76    14\n",
       "华东  7096.875000  113550    85    16\n",
       "华中  7585.714286   53100    33     7\n",
       "华北  8766.666667  105200    70    12\n",
       "华南  7011.111111  126200    84    18\n",
       "西北  7393.750000  118300    87    16\n",
       "西南  7800.875000  124814    95    16"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='地区').agg({'销售额':['mean','sum'],\n",
    "                          '数量（台）':['sum','count']})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaf4a6ef",
   "metadata": {},
   "source": [
    "## 透视表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ac2f29c2",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>家电品牌</th>\n",
       "      <th>奥克斯</th>\n",
       "      <th>志高</th>\n",
       "      <th>春兰</th>\n",
       "      <th>松下</th>\n",
       "      <th>格力</th>\n",
       "      <th>海尔</th>\n",
       "      <th>美的</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>东北</th>\n",
       "      <td>19200</td>\n",
       "      <td>17600</td>\n",
       "      <td>11100</td>\n",
       "      <td>13500</td>\n",
       "      <td>13500</td>\n",
       "      <td>9000</td>\n",
       "      <td>16205</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华东</th>\n",
       "      <td>10000</td>\n",
       "      <td>2000</td>\n",
       "      <td>15300</td>\n",
       "      <td>29400</td>\n",
       "      <td>34800</td>\n",
       "      <td>4500</td>\n",
       "      <td>17550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华中</th>\n",
       "      <td>12000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23100</td>\n",
       "      <td>7200</td>\n",
       "      <td>0</td>\n",
       "      <td>10800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华北</th>\n",
       "      <td>14400</td>\n",
       "      <td>0</td>\n",
       "      <td>4500</td>\n",
       "      <td>21000</td>\n",
       "      <td>17300</td>\n",
       "      <td>19500</td>\n",
       "      <td>28500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华南</th>\n",
       "      <td>9600</td>\n",
       "      <td>3000</td>\n",
       "      <td>6000</td>\n",
       "      <td>31800</td>\n",
       "      <td>27600</td>\n",
       "      <td>0</td>\n",
       "      <td>48200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西北</th>\n",
       "      <td>6000</td>\n",
       "      <td>8000</td>\n",
       "      <td>13500</td>\n",
       "      <td>12900</td>\n",
       "      <td>30000</td>\n",
       "      <td>25100</td>\n",
       "      <td>22800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西南</th>\n",
       "      <td>41207</td>\n",
       "      <td>22600</td>\n",
       "      <td>9600</td>\n",
       "      <td>18600</td>\n",
       "      <td>22007</td>\n",
       "      <td>0</td>\n",
       "      <td>10800</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "家电品牌    奥克斯     志高     春兰     松下     格力     海尔     美的\n",
       "地区                                                   \n",
       "东北    19200  17600  11100  13500  13500   9000  16205\n",
       "华东    10000   2000  15300  29400  34800   4500  17550\n",
       "华中    12000      0      0  23100   7200      0  10800\n",
       "华北    14400      0   4500  21000  17300  19500  28500\n",
       "华南     9600   3000   6000  31800  27600      0  48200\n",
       "西北     6000   8000  13500  12900  30000  25100  22800\n",
       "西南    41207  22600   9600  18600  22007      0  10800"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(data,\n",
    "              index='地区',\n",
    "              columns='家电品牌',\n",
    "              values='销售额',\n",
    "              aggfunc='sum',\n",
    "              fill_value=0) # 常用！！！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8865aa72",
   "metadata": {},
   "source": [
    "# 时间数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "55c56e8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('5124 days 15:30:44')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T1=pd.Timestamp(year=2023,\n",
    "            month=6,\n",
    "            day=29,\n",
    "            hour=9,\n",
    "            minute=39,\n",
    "            second=45)\n",
    "T2=pd.Timestamp(year=2009,\n",
    "            month=6,\n",
    "            day=17,\n",
    "            hour=18,\n",
    "            minute=9,\n",
    "            second=1)\n",
    "T1-T2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "44769dfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>数量（台）</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>月份</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>52000</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>51000</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>61700</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>71100</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>94650</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>100000</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>56805</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>76000</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>67100</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>55000</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>30914</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>25000</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       销售额  数量（台）\n",
       "月份               \n",
       "1    52000     36\n",
       "2    51000     35\n",
       "3    61700     48\n",
       "4    71100     48\n",
       "5    94650     67\n",
       "6   100000     66\n",
       "7    56805     38\n",
       "8    76000     51\n",
       "9    67100     53\n",
       "10   55000     46\n",
       "11   30914     23\n",
       "12   25000     19"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['月份']=data['销售日期'].dt.month\n",
    "data.groupby(by='月份')[['销售额','数量（台）']].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c4bd0d04",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>订单号</th>\n",
       "      <th>销售日期</th>\n",
       "      <th>销售人员</th>\n",
       "      <th>地区</th>\n",
       "      <th>城市</th>\n",
       "      <th>家电品牌</th>\n",
       "      <th>单价</th>\n",
       "      <th>数量（台）</th>\n",
       "      <th>销售额</th>\n",
       "      <th>月份</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>10287</td>\n",
       "      <td>2009-06-18</td>\n",
       "      <td>张锋</td>\n",
       "      <td>华北</td>\n",
       "      <td>石家庄</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>9</td>\n",
       "      <td>10800</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>10288</td>\n",
       "      <td>2009-06-19</td>\n",
       "      <td>张锋</td>\n",
       "      <td>华北</td>\n",
       "      <td>石家庄</td>\n",
       "      <td>美的</td>\n",
       "      <td>2000</td>\n",
       "      <td>7</td>\n",
       "      <td>14000</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>10289</td>\n",
       "      <td>2009-06-21</td>\n",
       "      <td>张三</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>松下</td>\n",
       "      <td>2100</td>\n",
       "      <td>10</td>\n",
       "      <td>21000</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>10290</td>\n",
       "      <td>2009-06-23</td>\n",
       "      <td>李四</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>8</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>10291</td>\n",
       "      <td>2009-06-24</td>\n",
       "      <td>大军</td>\n",
       "      <td>东北</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>5</td>\n",
       "      <td>6000</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>10292</td>\n",
       "      <td>2009-06-25</td>\n",
       "      <td>小莉</td>\n",
       "      <td>东北</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>格力</td>\n",
       "      <td>1300</td>\n",
       "      <td>3</td>\n",
       "      <td>3900</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>10293</td>\n",
       "      <td>2009-06-26</td>\n",
       "      <td>万西</td>\n",
       "      <td>东北</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>美的</td>\n",
       "      <td>1250</td>\n",
       "      <td>4</td>\n",
       "      <td>5000</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>10294</td>\n",
       "      <td>2009-07-04</td>\n",
       "      <td>彤彤</td>\n",
       "      <td>东北</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1500</td>\n",
       "      <td>5</td>\n",
       "      <td>7500</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>10295</td>\n",
       "      <td>2009-07-05</td>\n",
       "      <td>海利</td>\n",
       "      <td>东北</td>\n",
       "      <td>沈阳</td>\n",
       "      <td>海尔</td>\n",
       "      <td>1500</td>\n",
       "      <td>6</td>\n",
       "      <td>9000</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>10296</td>\n",
       "      <td>2009-07-06</td>\n",
       "      <td>全圈</td>\n",
       "      <td>东北</td>\n",
       "      <td>沈阳</td>\n",
       "      <td>美的</td>\n",
       "      <td>1400</td>\n",
       "      <td>3</td>\n",
       "      <td>4200</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>10297</td>\n",
       "      <td>2009-07-07</td>\n",
       "      <td>泉清</td>\n",
       "      <td>东北</td>\n",
       "      <td>沈阳</td>\n",
       "      <td>美的</td>\n",
       "      <td>1401</td>\n",
       "      <td>5</td>\n",
       "      <td>7005</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>10298</td>\n",
       "      <td>2009-07-08</td>\n",
       "      <td>萧逸</td>\n",
       "      <td>华东</td>\n",
       "      <td>杭州</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1500</td>\n",
       "      <td>6</td>\n",
       "      <td>9000</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>10299</td>\n",
       "      <td>2009-07-09</td>\n",
       "      <td>范伟</td>\n",
       "      <td>华东</td>\n",
       "      <td>杭州</td>\n",
       "      <td>海尔</td>\n",
       "      <td>1500</td>\n",
       "      <td>3</td>\n",
       "      <td>4500</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>10300</td>\n",
       "      <td>2009-07-14</td>\n",
       "      <td>周天</td>\n",
       "      <td>华东</td>\n",
       "      <td>杭州</td>\n",
       "      <td>美的</td>\n",
       "      <td>1400</td>\n",
       "      <td>2</td>\n",
       "      <td>2800</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>10301</td>\n",
       "      <td>2009-07-18</td>\n",
       "      <td>艾红</td>\n",
       "      <td>华东</td>\n",
       "      <td>杭州</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>10302</td>\n",
       "      <td>2009-07-22</td>\n",
       "      <td>饶伟</td>\n",
       "      <td>华东</td>\n",
       "      <td>上海</td>\n",
       "      <td>美的</td>\n",
       "      <td>2000</td>\n",
       "      <td>4</td>\n",
       "      <td>8000</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>10303</td>\n",
       "      <td>2009-08-26</td>\n",
       "      <td>李乐</td>\n",
       "      <td>华东</td>\n",
       "      <td>上海</td>\n",
       "      <td>松下</td>\n",
       "      <td>2100</td>\n",
       "      <td>2</td>\n",
       "      <td>4200</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>10304</td>\n",
       "      <td>2009-08-01</td>\n",
       "      <td>谢九</td>\n",
       "      <td>华东</td>\n",
       "      <td>上海</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>10305</td>\n",
       "      <td>2009-08-08</td>\n",
       "      <td>李二</td>\n",
       "      <td>华东</td>\n",
       "      <td>上海</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>11</td>\n",
       "      <td>13200</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>10306</td>\n",
       "      <td>2009-08-12</td>\n",
       "      <td>小花</td>\n",
       "      <td>华南</td>\n",
       "      <td>厦门</td>\n",
       "      <td>美的</td>\n",
       "      <td>1400</td>\n",
       "      <td>8</td>\n",
       "      <td>11200</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>10307</td>\n",
       "      <td>2009-08-13</td>\n",
       "      <td>小米</td>\n",
       "      <td>华南</td>\n",
       "      <td>厦门</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>3</td>\n",
       "      <td>3600</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>10308</td>\n",
       "      <td>2009-08-14</td>\n",
       "      <td>李明</td>\n",
       "      <td>华南</td>\n",
       "      <td>厦门</td>\n",
       "      <td>美的</td>\n",
       "      <td>2000</td>\n",
       "      <td>3</td>\n",
       "      <td>6000</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>10309</td>\n",
       "      <td>2009-08-15</td>\n",
       "      <td>张虹</td>\n",
       "      <td>华南</td>\n",
       "      <td>厦门</td>\n",
       "      <td>松下</td>\n",
       "      <td>2100</td>\n",
       "      <td>10</td>\n",
       "      <td>21000</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>10310</td>\n",
       "      <td>2009-08-16</td>\n",
       "      <td>张虹</td>\n",
       "      <td>华南</td>\n",
       "      <td>厦门</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>6</td>\n",
       "      <td>7200</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>10311</td>\n",
       "      <td>2009-08-27</td>\n",
       "      <td>罗伊</td>\n",
       "      <td>华南</td>\n",
       "      <td>广州</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>10312</td>\n",
       "      <td>2009-09-10</td>\n",
       "      <td>罗伦</td>\n",
       "      <td>华南</td>\n",
       "      <td>广州</td>\n",
       "      <td>志高</td>\n",
       "      <td>1000</td>\n",
       "      <td>3</td>\n",
       "      <td>3000</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>10313</td>\n",
       "      <td>2009-09-11</td>\n",
       "      <td>汤红</td>\n",
       "      <td>华南</td>\n",
       "      <td>广州</td>\n",
       "      <td>美的</td>\n",
       "      <td>1350</td>\n",
       "      <td>4</td>\n",
       "      <td>5400</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>74</th>\n",
       "      <td>10314</td>\n",
       "      <td>2009-09-12</td>\n",
       "      <td>王维</td>\n",
       "      <td>华南</td>\n",
       "      <td>广州</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1200</td>\n",
       "      <td>5</td>\n",
       "      <td>6000</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>10315</td>\n",
       "      <td>2009-09-13</td>\n",
       "      <td>罗伊</td>\n",
       "      <td>华南</td>\n",
       "      <td>广州</td>\n",
       "      <td>松下</td>\n",
       "      <td>1200</td>\n",
       "      <td>2</td>\n",
       "      <td>2400</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>10316</td>\n",
       "      <td>2009-09-14</td>\n",
       "      <td>青青</td>\n",
       "      <td>东北</td>\n",
       "      <td>长春</td>\n",
       "      <td>松下</td>\n",
       "      <td>1500</td>\n",
       "      <td>9</td>\n",
       "      <td>13500</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>10317</td>\n",
       "      <td>2009-09-15</td>\n",
       "      <td>小娟</td>\n",
       "      <td>东北</td>\n",
       "      <td>长春</td>\n",
       "      <td>志高</td>\n",
       "      <td>1400</td>\n",
       "      <td>9</td>\n",
       "      <td>12600</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>10318</td>\n",
       "      <td>2009-09-16</td>\n",
       "      <td>李娜</td>\n",
       "      <td>东北</td>\n",
       "      <td>长春</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1200</td>\n",
       "      <td>3</td>\n",
       "      <td>3600</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>10319</td>\n",
       "      <td>2009-09-17</td>\n",
       "      <td>小莉</td>\n",
       "      <td>东北</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>5</td>\n",
       "      <td>6000</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>10320</td>\n",
       "      <td>2009-09-18</td>\n",
       "      <td>万西</td>\n",
       "      <td>东北</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>8</td>\n",
       "      <td>9600</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>81</th>\n",
       "      <td>10321</td>\n",
       "      <td>2009-09-19</td>\n",
       "      <td>彤彤</td>\n",
       "      <td>东北</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>志高</td>\n",
       "      <td>1000</td>\n",
       "      <td>5</td>\n",
       "      <td>5000</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>10322</td>\n",
       "      <td>2009-10-13</td>\n",
       "      <td>泉清</td>\n",
       "      <td>东北</td>\n",
       "      <td>沈阳</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>6</td>\n",
       "      <td>7200</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>10323</td>\n",
       "      <td>2009-10-14</td>\n",
       "      <td>龚方</td>\n",
       "      <td>西北</td>\n",
       "      <td>乌鲁木齐</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>3</td>\n",
       "      <td>3600</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>10324</td>\n",
       "      <td>2009-10-15</td>\n",
       "      <td>张强</td>\n",
       "      <td>西北</td>\n",
       "      <td>乌鲁木齐</td>\n",
       "      <td>志高</td>\n",
       "      <td>1000</td>\n",
       "      <td>4</td>\n",
       "      <td>4000</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>10325</td>\n",
       "      <td>2009-10-16</td>\n",
       "      <td>易路</td>\n",
       "      <td>西北</td>\n",
       "      <td>乌鲁木齐</td>\n",
       "      <td>海尔</td>\n",
       "      <td>1500</td>\n",
       "      <td>2</td>\n",
       "      <td>3000</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>10326</td>\n",
       "      <td>2009-10-17</td>\n",
       "      <td>刘强</td>\n",
       "      <td>西北</td>\n",
       "      <td>乌鲁木齐</td>\n",
       "      <td>海尔</td>\n",
       "      <td>1400</td>\n",
       "      <td>4</td>\n",
       "      <td>5600</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>10327</td>\n",
       "      <td>2009-10-18</td>\n",
       "      <td>曹容</td>\n",
       "      <td>西北</td>\n",
       "      <td>兰州</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>10</td>\n",
       "      <td>12000</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>10328</td>\n",
       "      <td>2009-10-19</td>\n",
       "      <td>蒋艳</td>\n",
       "      <td>西北</td>\n",
       "      <td>兰州</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>5</td>\n",
       "      <td>6000</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>89</th>\n",
       "      <td>10329</td>\n",
       "      <td>2009-10-20</td>\n",
       "      <td>李辉</td>\n",
       "      <td>西北</td>\n",
       "      <td>兰州</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>8</td>\n",
       "      <td>9600</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>90</th>\n",
       "      <td>10330</td>\n",
       "      <td>2009-10-21</td>\n",
       "      <td>欧良</td>\n",
       "      <td>西北</td>\n",
       "      <td>兰州</td>\n",
       "      <td>志高</td>\n",
       "      <td>1000</td>\n",
       "      <td>4</td>\n",
       "      <td>4000</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>91</th>\n",
       "      <td>10331</td>\n",
       "      <td>2009-11-12</td>\n",
       "      <td>王刚</td>\n",
       "      <td>西南</td>\n",
       "      <td>重庆</td>\n",
       "      <td>格力</td>\n",
       "      <td>1200</td>\n",
       "      <td>5</td>\n",
       "      <td>6000</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>10332</td>\n",
       "      <td>2009-11-15</td>\n",
       "      <td>赵军</td>\n",
       "      <td>西南</td>\n",
       "      <td>重庆</td>\n",
       "      <td>格力</td>\n",
       "      <td>1201</td>\n",
       "      <td>7</td>\n",
       "      <td>8407</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>10333</td>\n",
       "      <td>2009-11-16</td>\n",
       "      <td>小李</td>\n",
       "      <td>西南</td>\n",
       "      <td>重庆</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1500</td>\n",
       "      <td>4</td>\n",
       "      <td>6000</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>10334</td>\n",
       "      <td>2009-11-25</td>\n",
       "      <td>王娜</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1501</td>\n",
       "      <td>7</td>\n",
       "      <td>10507</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>10335</td>\n",
       "      <td>2009-12-01</td>\n",
       "      <td>刘宇</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>格力</td>\n",
       "      <td>1400</td>\n",
       "      <td>2</td>\n",
       "      <td>2800</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>10336</td>\n",
       "      <td>2009-12-12</td>\n",
       "      <td>陈笑</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>志高</td>\n",
       "      <td>1400</td>\n",
       "      <td>9</td>\n",
       "      <td>12600</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>10337</td>\n",
       "      <td>2009-12-23</td>\n",
       "      <td>汪俊</td>\n",
       "      <td>西南</td>\n",
       "      <td>昆明</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1200</td>\n",
       "      <td>3</td>\n",
       "      <td>3600</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>10338</td>\n",
       "      <td>2009-12-26</td>\n",
       "      <td>齐易</td>\n",
       "      <td>西南</td>\n",
       "      <td>昆明</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>5</td>\n",
       "      <td>6000</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      订单号       销售日期 销售人员  地区    城市 家电品牌    单价  数量（台）    销售额  月份\n",
       "47  10287 2009-06-18   张锋  华北   石家庄   格力  1200      9  10800   6\n",
       "48  10288 2009-06-19   张锋  华北   石家庄   美的  2000      7  14000   6\n",
       "49  10289 2009-06-21   张三  华北    北京   松下  2100     10  21000   6\n",
       "50  10290 2009-06-23   李四  华北    北京  奥克斯  1200      8   9600   6\n",
       "51  10291 2009-06-24   大军  东北   哈尔滨  奥克斯  1200      5   6000   6\n",
       "52  10292 2009-06-25   小莉  东北   哈尔滨   格力  1300      3   3900   6\n",
       "53  10293 2009-06-26   万西  东北   哈尔滨   美的  1250      4   5000   6\n",
       "54  10294 2009-07-04   彤彤  东北   哈尔滨   春兰  1500      5   7500   7\n",
       "55  10295 2009-07-05   海利  东北    沈阳   海尔  1500      6   9000   7\n",
       "56  10296 2009-07-06   全圈  东北    沈阳   美的  1400      3   4200   7\n",
       "57  10297 2009-07-07   泉清  东北    沈阳   美的  1401      5   7005   7\n",
       "58  10298 2009-07-08   萧逸  华东    杭州   春兰  1500      6   9000   7\n",
       "59  10299 2009-07-09   范伟  华东    杭州   海尔  1500      3   4500   7\n",
       "60  10300 2009-07-14   周天  华东    杭州   美的  1400      2   2800   7\n",
       "61  10301 2009-07-18   艾红  华东    杭州   格力  1200      4   4800   7\n",
       "62  10302 2009-07-22   饶伟  华东    上海   美的  2000      4   8000   7\n",
       "63  10303 2009-08-26   李乐  华东    上海   松下  2100      2   4200   8\n",
       "64  10304 2009-08-01   谢九  华东    上海  奥克斯  1200      4   4800   8\n",
       "65  10305 2009-08-08   李二  华东    上海   格力  1200     11  13200   8\n",
       "66  10306 2009-08-12   小花  华南    厦门   美的  1400      8  11200   8\n",
       "67  10307 2009-08-13   小米  华南    厦门   格力  1200      3   3600   8\n",
       "68  10308 2009-08-14   李明  华南    厦门   美的  2000      3   6000   8\n",
       "69  10309 2009-08-15   张虹  华南    厦门   松下  2100     10  21000   8\n",
       "70  10310 2009-08-16   张虹  华南    厦门  奥克斯  1200      6   7200   8\n",
       "71  10311 2009-08-27   罗伊  华南    广州   格力  1200      4   4800   8\n",
       "72  10312 2009-09-10   罗伦  华南    广州   志高  1000      3   3000   9\n",
       "73  10313 2009-09-11   汤红  华南    广州   美的  1350      4   5400   9\n",
       "74  10314 2009-09-12   王维  华南    广州   春兰  1200      5   6000   9\n",
       "75  10315 2009-09-13   罗伊  华南    广州   松下  1200      2   2400   9\n",
       "76  10316 2009-09-14   青青  东北    长春   松下  1500      9  13500   9\n",
       "77  10317 2009-09-15   小娟  东北    长春   志高  1400      9  12600   9\n",
       "78  10318 2009-09-16   李娜  东北    长春   春兰  1200      3   3600   9\n",
       "79  10319 2009-09-17   小莉  东北   哈尔滨  奥克斯  1200      5   6000   9\n",
       "80  10320 2009-09-18   万西  东北   哈尔滨   格力  1200      8   9600   9\n",
       "81  10321 2009-09-19   彤彤  东北   哈尔滨   志高  1000      5   5000   9\n",
       "82  10322 2009-10-13   泉清  东北    沈阳  奥克斯  1200      6   7200  10\n",
       "83  10323 2009-10-14   龚方  西北  乌鲁木齐   格力  1200      3   3600  10\n",
       "84  10324 2009-10-15   张强  西北  乌鲁木齐   志高  1000      4   4000  10\n",
       "85  10325 2009-10-16   易路  西北  乌鲁木齐   海尔  1500      2   3000  10\n",
       "86  10326 2009-10-17   刘强  西北  乌鲁木齐   海尔  1400      4   5600  10\n",
       "87  10327 2009-10-18   曹容  西北    兰州   格力  1200     10  12000  10\n",
       "88  10328 2009-10-19   蒋艳  西北    兰州  奥克斯  1200      5   6000  10\n",
       "89  10329 2009-10-20   李辉  西北    兰州   格力  1200      8   9600  10\n",
       "90  10330 2009-10-21   欧良  西北    兰州   志高  1000      4   4000  10\n",
       "91  10331 2009-11-12   王刚  西南    重庆   格力  1200      5   6000  11\n",
       "92  10332 2009-11-15   赵军  西南    重庆   格力  1201      7   8407  11\n",
       "93  10333 2009-11-16   小李  西南    重庆  奥克斯  1500      4   6000  11\n",
       "94  10334 2009-11-25   王娜  西南    成都  奥克斯  1501      7  10507  11\n",
       "95  10335 2009-12-01   刘宇  西南    成都   格力  1400      2   2800  12\n",
       "96  10336 2009-12-12   陈笑  西南    成都   志高  1400      9  12600  12\n",
       "97  10337 2009-12-23   汪俊  西南    昆明   春兰  1200      3   3600  12\n",
       "98  10338 2009-12-26   齐易  西南    昆明  奥克斯  1200      5   6000  12"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index=data['销售日期']>T2\n",
    "data[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4a964217",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7055 days 09:39:45\n",
      "9152 days 09:39:45\n",
      "6993 days 09:39:45\n"
     ]
    }
   ],
   "source": [
    "list1=['2004-03-05','1998.06.08','2004/5/6']\n",
    "list1=pd.to_datetime(list1) \n",
    "for i in list1:\n",
    "    print(T1-i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce6c28e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "data=pd.read_excel('Excel数据.xlsx',\n",
    "                   parse_dates=['销售日期']# 强制转换时间格式\n",
    "                  )\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d5156bf7",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['销售日期']=pd.to_datetime(data['销售日期'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
